package com.sgb.kjwl.view.ui.activity.self.packers;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfTeamAlbumEditBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfTeamAlbumEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfTeamAlbumEditAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.packers.SelfTeamAlbumEditModel;
import com.swgk.core.base.di.AppComponent;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

/**
 * @author liwei
 * @date 2020/10/13
 * description:工队相册
 */
public class SelfTeamAlbumEditActivity extends AppActivity {

    private AtSelfTeamAlbumEditBinding binding;
    private SpannableStringBuilder spend;
    private List<SelfTeamAlbumEntity> photoList;
    private SelfTeamAlbumEditAdapter editAdapter;
    private boolean isAll = false;

    @Inject
    SelfTeamAlbumEditModel viewModel;


    public static void start(Context context, List<SelfTeamAlbumEntity> list){
        Intent intent = new Intent(context, SelfTeamAlbumEditActivity.class);
        intent.putExtra("list",(Serializable) list);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_team_album_edit);
        binding.actionBarImageRight.setOnClickListener(view->finish());
        binding.selectAll.setOnClickListener(view -> {
            isAll = !isAll;
            setSelectNum(isAll?photoList.size():0);
            editAdapter.setSelectAll(isAll);
        });

        binding.deletePhoto.setOnClickListener(view -> deleteDialog());

        spend = new SpannableStringBuilder("");
    }

    @Override
    protected void init() {
        photoList = (List<SelfTeamAlbumEntity>)getIntent().getSerializableExtra("list");

        GridLayoutManager manager = new GridLayoutManager(this,3);
        editAdapter = new SelfTeamAlbumEditAdapter(this,photoList, Glide.with(this), manager);
        binding.listView.setLayoutManager(manager);
        binding.listView.setAdapter(editAdapter);

        editAdapter.setSelectListener(urls -> setSelectNum(urls.size()));
    }

    private void setSelectNum(int size){
        String str = "(已选择"+ size+"张图片)";
        spend.clear();
        spend.append(str);
        spend.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this,R.color.new_org)),4,str.indexOf("张"), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        binding.selectNum.setText(spend);
        Drawable allDrawable;
        if(size == photoList.size()){
            isAll = true;
            allDrawable = ContextCompat.getDrawable(this,R.mipmap.common_checked);
        }else{
            isAll = false;
            allDrawable = ContextCompat.getDrawable(this,R.mipmap.commom_uncheck);
        }
        if (allDrawable != null) {
            allDrawable.setBounds(0,0,allDrawable.getMinimumWidth(),allDrawable.getMinimumHeight());
        }
        binding.selectAll.setCompoundDrawables(allDrawable,null,null,null);

        if(size>0){
            binding.deletePhoto.setTextColor(ContextCompat.getColor(this,R.color.new_org));
            binding.deletePhoto.setEnabled(true);
        }else{
            binding.deletePhoto.setEnabled(false);
            binding.deletePhoto.setTextColor(ContextCompat.getColor(this,R.color.color_969696));
        }
    }

    /**
     * 删除弹窗
     */
    private void deleteDialog() {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        // 设置点击屏幕Dialog不消失
        roundCornerDialog.setCanceledOnTouchOutside(false);

        TextView tvMessage = view.findViewById(R.id.tv_message);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvLogoutConfirm = view.findViewById(R.id.tv_logout_confirm);
        tvTitle.setVisibility(View.VISIBLE);
        TextView tvLogoutCancel = view.findViewById(R.id.tv_logout_cancel);
        tvTitle.setText("温馨提示");
        tvMessage.setText(String.format(Locale.CHINA,"您确定删除所选%d照片?",editAdapter.getSelectedUrls().size()));

        //确定
        tvLogoutConfirm.setOnClickListener(v -> {
            //删除
            viewModel.deletePhotoData(editAdapter.getSelectedUrls());
            roundCornerDialog.dismiss();
        });

        //取消
        tvLogoutCancel.setOnClickListener(v -> roundCornerDialog.dismiss());
    }
}
